﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using QuTouTiaoAssist.Entity;
using QuTouTiaoAssist.Util;

namespace QuTouTiaoAssist
{
    public partial class FrmMission : FrmBase
    {
        public FrmMission()
        {
            InitializeComponent();
            ClearControls();
        }

        private void bntRefresh_Click(object sender, EventArgs e)
        {
            if (!CheckLogin())
                return;

            ClearControls();
            new Thread(() =>
            {
                try
                {
                    var info = QttHelper.GetMessionList(base.MemeberToken);
                    base.InvokeCrossThread<QttResultInfo<QttFresherMissionInfo>>(Info2Controls, info);
                }
                catch (Exception ex)
                {
                    MsgBox.ShowInfo("查询失败，" + ex.Message);
                }
            }).Start();
        }

        private void Info2Controls(QttResultInfo<QttFresherMissionInfo> info)
        {
            foreach (var item in info.Data.Daily)
            {
                var row = new ListViewItem(new string[]
                {
                    item.Name,
                    item.Amount.ToString(),
                    item.Limit.ToString(),
                    item.MaxReward.ToString(),
                    item.Desc,
                });
                this.listView1.Groups[0].Items.Add(row);
                this.listView1.Items.Add(row);
            }
            foreach (var item in info.Data.Once)
            {
                var row = new ListViewItem(new string[]
                {
                    item.Name,
                    item.Amount.ToString(),
                    item.Limit.ToString(),
                    item.MaxReward.ToString(),
                    item.Desc,
                });
                this.listView1.Groups[1].Items.Add(row);
                this.listView1.Items.Add(row);
            }
            this.label1.Text = string.Format("已连续签到{0}天", info.Data.SignIn.Continuation);

            if (info.Data.TreasureBox.Next!=null)
            {
                var nextOpenTreasureTime = OtherUtil.UnixTimeInSeconds2DateTime(info.Data.TreasureBox.Next.Value);
                this.label2.Text = string.Format("下次开宝箱时间：{0} 距离下次开宝箱还有：{1}",
                        nextOpenTreasureTime,
                        (nextOpenTreasureTime - DateTime.Now).ToString()); 
            }
            else
            {
                this.label2.Text = "可以开宝箱了!";
            }
        }

        private void ClearControls()
        {
            this.listView1.Items.Clear();
            foreach (ListViewGroup group in this.listView1.Groups)
            {
                group.Items.Clear();
            }
            this.label1.Text = "";
            this.label2.Text = "";
        }
    }
}
